
  const layer = layui.layer
  // 获取用户的信息
  getUserInfo()
  function getUserInfo (){
    $.ajax({
      method:'get',
      url:'/my/userinfo',
      success:function(res) {
        // console.log(res);
        if (res.status !== 0) {
          return layer.msg('获取用户信息失败')
        }
        //调用渲染函数
        renderAvatar(res.data)
      },
    })
  }
  //染用户信息(昵称+头像)
  function renderAvatar(user) {
    let name = user.nickname || user.username
    $('#welcome').html(`欢迎 &nbsp;${name}`)

    if(user.user_pic !== null) {
      $('.layui-nav-img').attr('src',user.user_pic).show()
      $('.text-avatar').hide()
    }else {
      $('.layui-nav-img').hide()
      const first = name[0].toUpperCase()
      $('.text-avatar').html(first).show()
    }
  }

  // 点击按钮，实现退出功能
  $('#logOut').on('click', function(e) {
  e.preventDefault()
  layer.confirm('确定退出登录?', { icon: 3, title: '提示' }, function(index) {
    localStorage.removeItem('token')
    location.href = '/login.html'
    layer.close(index)
  })
})
